package com.googlecode.curvedmetromaps.core.logic.costfunction;

import java.util.HashMap;

import com.googlecode.curvedmetromaps.core.model.MetroMap;

public abstract class BaseCost {
	public final static String			name	= "Base Cost";

	protected MetroMap					map;
	public HashMap<String, CostWeight>	weights;

	/**
	 * Instantiates a cost function.
	 * @param metroMap Model of the metro map.
	 */
	public BaseCost(MetroMap metroMap) {
		this.map = metroMap;
		this.weights = new HashMap<String, CostWeight>();
	}

	/**
	 * Returns the friendly name of the cost function.
	 */
	public abstract String getName();

	/**
	 * Calculate a cost value from the metro map.
	 */
	public abstract double calculateCost();

	/**
	 * Creates weight (removes one if already present using this key).
	 * @param key The key under which the weight is to be stored.
	 */
	protected void createWeight(String key, String label, double value) {
		if (weights.containsKey(key))
			weights.remove(key);

		weights.put(key, new CostWeight(label, value));
	}	

	/**
	 * Sets or adds a weight
	 * @param key The key under which the weight is stored.
	 */
	protected void setWeight(String key, double value) {
		if (weights.containsKey(key))
			weights.get(key).value = value;
		else
			weights.put(key, new CostWeight("", value));
	}	

	/**
	 * Gets the value of a weight
	 * @param key The key under which the weight is stored.
	 */
	protected double getWeight(String key) {
		return weights.get(key).value;
	}	
}
